package com.world.tg.service.impl;

import com.world.tg.dao.HomeDao;
import com.world.tg.pojo.Book;
import com.world.tg.pojo.Page;
import com.world.tg.result.JsonResult;
import com.world.tg.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeDao homeDao;

    @Override
    public JsonResult findHome(Double downPrice,Double upPrice,String name,Integer currentPage) {
        Page page = new Page(homeDao.findTotal(downPrice,upPrice,name));
        Integer offset = (currentPage-1)*Page.num;
        Map<String, Object> map = new HashMap<>();
        List<Book> books = homeDao.findBooksByNameOrPrice(downPrice,upPrice,name,currentPage,Page.num);
        map.put("pages",page);
        map.put("books",books);
        JsonResult jsonResult = new JsonResult();
        jsonResult.success("success",map);
        return jsonResult;
    }
}
